package top.fangw.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.f4b6a3.ulid.Ulid;
import com.github.f4b6a3.ulid.UlidCreator;
import lombok.RequiredArgsConstructor;
import top.fangw.admin.mapper.SysRoleJoinApplicationMapper;
import top.fangw.admin.mapper.SysRoleJoinMenuMapper;
import top.fangw.admin.pojo.entity.SysRole;
import top.fangw.admin.mapper.SysRoleMapper;
import top.fangw.admin.pojo.entity.SysRoleJoinApplication;
import top.fangw.admin.pojo.entity.SysRoleJoinMenu;
import top.fangw.admin.service.ISysRoleJoinApplicationService;
import top.fangw.admin.service.ISysRoleJoinMenuService;
import top.fangw.admin.service.ISysRoleJoinUserService;
import top.fangw.admin.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.common.util.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import top.fangw.admin.mapper.SysRoleMapper;
import top.fangw.admin.pojo.dto.RoleDto;
import top.fangw.admin.pojo.entity.SysRole;
import top.fangw.admin.service.ISysRoleJoinMenuService;
import top.fangw.admin.service.ISysRoleService;
import top.fangw.common.api.vo.InitRoleVo;
import top.fangw.common.api.vo.*;
import top.fangw.common.core.entity.BaseIdNameVo;
import top.fangw.common.mybatis.annotation.DyDataSource;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用途：用于存放系统的角色信息 服务实现类
 * </p>
 *
 * @author fangw
 * @since 2024-03-02
 */
@Service
@DS("base_info")
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {

    private final ISysRoleJoinUserService sysRoleJoinUserService;

    private final SysRoleJoinApplicationMapper sysRoleJoinApplicationMapper;

    private final SysRoleJoinMenuMapper sysRoleJoinMenuMapper;

    private final ISysRoleJoinMenuService sysRoleJoinMenuService;

    @Override
    @DyDataSource(tenantId = "#initRoleVo.tenantId", autoTenantId = false)
    public InitRoleVo initTenantRole(InitRoleVo initRoleVo) {
        // 初始化角色信息
        SysRole sysRole = BeanUtil.copyProperties(initRoleVo, SysRole.class);
        this.save(sysRole);
        // 初始化用户与角色的关联关系
        InitUserJoinRoleVo initUserJoinRoleVo = new InitUserJoinRoleVo(initRoleVo.getId(), initRoleVo.getAdminUserId(), initRoleVo.getTenantId());
        sysRoleJoinUserService.initUserJoinRole(initUserJoinRoleVo);
        // 初始化角色与应用的关联关系
        List<SysRoleJoinApplication> collect = initRoleVo.getInitApplicationVoList().stream().map(item -> {
            SysRoleJoinApplication sysRoleJoinApplication = new SysRoleJoinApplication();
            sysRoleJoinApplication.setRoleId(sysRole.getId());
            sysRoleJoinApplication.setApplicationId(item.getId());
            sysRoleJoinApplication.setTenantId(initRoleVo.getTenantId());
            return sysRoleJoinApplication;
        }).toList();
        sysRoleJoinApplicationMapper.insertBatch(collect);
        // 初始化角色与菜单的关联关系
        List<SysRoleJoinMenu> joinMenus = initRoleVo.getInitRoleJoinMenuList().stream().map(item -> {
            SysRoleJoinMenu sysRoleJoinMenu = new SysRoleJoinMenu();
            sysRoleJoinMenu.setRoleId(sysRole.getId());
            sysRoleJoinMenu.setMenuId(item.getId());
            sysRoleJoinMenu.setTenantId(initRoleVo.getTenantId());
            return sysRoleJoinMenu;
        }).toList();
        sysRoleJoinMenuMapper.insertBatch(joinMenus);
        return null;
    }

    @Override
    @DyDataSource()
    @Transactional(rollbackFor = Exception.class)
    public void add(SysRole sysRole) {
        // 添加角色基础信息
        this.baseMapper.insert(sysRole);
        // 添加应用与角色的关联关系
        List<String> applicationIds = sysRole.getApplicationIds();
        Assert.notEmpty(applicationIds, "应用信息不能为空");
        List<SysRoleJoinApplication> sysRoleJoinApplicationList = new ArrayList<>();
        applicationIds.forEach(item -> {
            SysRoleJoinApplication sysRoleJoinApplication = new SysRoleJoinApplication();
            sysRoleJoinApplication.setApplicationId(item);
            sysRoleJoinApplication.setRoleId(sysRole.getId());
            sysRoleJoinApplicationList.add(sysRoleJoinApplication);
        });
        if (sysRoleJoinApplicationList.size() > 0) {
            sysRoleJoinApplicationMapper.insertBatch(sysRoleJoinApplicationList);
        }
        // 添加菜单与角色的关联关系
        List<BaseIdNameVo> menuIds = sysRole.getMenuIds();
        Assert.notEmpty(menuIds, "菜单信息不能为空");
        List<SysRoleJoinMenu> sysRoleJoinMenuList = new ArrayList<>();
        menuIds.forEach(item -> {
            SysRoleJoinMenu sysRoleJoinMenu = new SysRoleJoinMenu();
            sysRoleJoinMenu.setMenuId(item.getId());
            sysRoleJoinMenu.setRoleId(sysRole.getId());
            sysRoleJoinMenu.setMenuPath(item.getName());
            sysRoleJoinMenu.setDataScope(item.getValue());
            sysRoleJoinMenuList.add(sysRoleJoinMenu);
        });
        if (sysRoleJoinMenuList.size() > 0) {
            sysRoleJoinMenuService.saveBatch(sysRoleJoinMenuList);
        }
    }
}
